package org.jeecg.modules.configs.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.configs.entity.BlogConfigs;
import org.jeecg.modules.configs.service.BlogConfigsService;
import org.jeecg.config.shiro.IgnoreAuth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 博客配置管理
 * @Author: jeecg-boot
 * @Date: 2025-06-30
 * @Version: V1.0
 */
@Tag(name = "博客配置管理")
@RestController
@RequestMapping("/blog/configs")
@Slf4j
public class BlogConfigsController {
	@Autowired
	private BlogConfigsService blogConfigsService;
	
	/**
	 * 获取所有配置
	 *
	 * @return
	 */
	@Operation(summary = "获取所有配置")
	@GetMapping(value = "/")
	@IgnoreAuth
	public Result<List<BlogConfigs>> getAllConfigs() {
		QueryWrapper<BlogConfigs> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", 1).orderByAsc("sort_order");
		List<BlogConfigs> configs = blogConfigsService.list(queryWrapper);
		return Result.OK(configs);
	}
	
	/**
	 * 根据页面类型获取配置
	 *
	 * @param pageType
	 * @return
	 */
	@Operation(summary = "根据页面类型获取配置")
	@GetMapping(value = "/page")
	@IgnoreAuth
	public Result<List<BlogConfigs>> getConfigsByPageType(@RequestParam(name = "pageType", required = true) String pageType) {
		QueryWrapper<BlogConfigs> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("page_type", pageType)
				   .eq("status", 1)
				   .orderByAsc("sort_order");
		List<BlogConfigs> configs = blogConfigsService.list(queryWrapper);
		return Result.OK(configs);
	}
	
	/**
	 * 根据配置键获取配置
	 *
	 * @param configKey
	 * @return
	 */
	@Operation(summary = "根据配置键获取配置")
	@GetMapping(value = "/key")
	@IgnoreAuth
	public Result<BlogConfigs> getConfigByKey(@RequestParam(name = "configKey", required = true) String configKey) {
		QueryWrapper<BlogConfigs> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("config_key", configKey)
				   .eq("status", 1);
		BlogConfigs config = blogConfigsService.getOne(queryWrapper);
		if (config == null) {
			return Result.error("未找到对应配置");
		}
		return Result.OK(config);
	}

	/**
	 * 分页列表查询
	 *
	 * @param blogConfigs
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "blog_configs-分页列表查询")
	@Operation(summary="blog_configs-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BlogConfigs>> queryPageList(BlogConfigs blogConfigs,
													@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													HttpServletRequest req) {
		QueryWrapper<BlogConfigs> queryWrapper = QueryGenerator.initQueryWrapper(blogConfigs, req.getParameterMap());
		Page<BlogConfigs> page = new Page<BlogConfigs>(pageNo, pageSize);
		IPage<BlogConfigs> pageList = blogConfigsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param blogConfigs
	 * @return
	 */
	@AutoLog(value = "blog_configs-添加")
	@Operation(summary="blog_configs-添加")
	@RequiresPermissions("configs:blog_configs:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BlogConfigs blogConfigs) {
		blogConfigsService.save(blogConfigs);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param blogConfigs
	 * @return
	 */
	@AutoLog(value = "blog_configs-编辑")
	@Operation(summary="blog_configs-编辑")
	@RequiresPermissions("configs:blog_configs:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BlogConfigs blogConfigs) {
		blogConfigsService.updateById(blogConfigs);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "blog_configs-通过id删除")
	@Operation(summary="blog_configs-通过id删除")
	@RequiresPermissions("configs:blog_configs:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		blogConfigsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "blog_configs-批量删除")
	@Operation(summary="blog_configs-批量删除")
	@RequiresPermissions("configs:blog_configs:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.blogConfigsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "blog_configs-通过id查询")
	@Operation(summary="blog_configs-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BlogConfigs> queryById(@RequestParam(name="id",required=true) String id) {
		BlogConfigs blogConfigs = blogConfigsService.getById(id);
		if(blogConfigs==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(blogConfigs);
	}

}
